﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 创建者：王明亮
 * 创建时间：2022/8/14 23:13:29
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------
 */
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;


namespace Aliang.Common.Helper.Config
{
    /// <summary>
    /// IniFile 的摘要说明
    /// </summary>
    public class IniFile
    {
        private string m_FileName;

        public string FileName
        {
            get
            {
                return m_FileName;
            }
            set
            {
                m_FileName = value;
            }
        }

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

        [DllImport("kernel32.dll")]
        private static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);

        public IniFile(string aFileName)
        {
            m_FileName = aFileName;
        }

        public IniFile()
        {
        }

        public int ReadInt(string section, string name, int def)
        {
            return GetPrivateProfileInt(section, name, def, m_FileName);
        }

        public string ReadString(string section, string name, string def)
        {
            StringBuilder stringBuilder = new StringBuilder(2048);
            GetPrivateProfileString(section, name, def, stringBuilder, 2048, m_FileName);
            return stringBuilder.ToString();
        }

        public void WriteInt(string section, string name, int Ival)
        {
            WritePrivateProfileString(section, name, Ival.ToString(), m_FileName);
        }

        public void WriteString(string section, string name, string strVal)
        {
            WritePrivateProfileString(section, name, strVal, m_FileName);
        }

        public void DeleteSection(string section)
        {
            WritePrivateProfileString(section, null, null, m_FileName);
        }

        public void DeleteAllSection()
        {
            WritePrivateProfileString(null, null, null, m_FileName);
        }

        public string IniReadValue(string section, string name)
        {
            StringBuilder stringBuilder = new StringBuilder(256);
            GetPrivateProfileString(section, name, "", stringBuilder, 256, m_FileName);
            return stringBuilder.ToString();
        }

        public void IniWriteValue(string section, string name, string value)
        {
            WritePrivateProfileString(section, name, value, m_FileName);
        }
    }
}
